<template>
  <div class="create-group">
    <el-dialog
      title="创建新群组"
      :visible.sync="dialogVisible"
      width="500px"
      :before-close="handleClose"
    >
      <el-form :model="groupForm" :rules="rules" ref="groupForm" label-width="80px">
        <el-form-item label="群组名称" prop="groupName">
          <el-input v-model="groupForm.groupName" placeholder="请输入群组名称"></el-input>
        </el-form-item>
        
        <el-form-item label="群组描述" prop="descText">
          <el-input
            type="textarea"
            v-model="groupForm.descText"
            placeholder="请输入群组描述"
            :rows="3"
          ></el-input>
        </el-form-item>

        <el-form-item label="选择班级" prop="classId">
          <el-select
            v-model="groupForm.classId"
            placeholder="请选择班级（可选）"
            style="width: 100%"
            clearable
            value-key="classId"
          >
            <el-option
              v-for="classItem in classList"
              :key="classItem.classId"
              :label="classItem.className"
              :value="classItem.classId"
            >
              <span style="float: left">{{ classItem.className }}</span>
              <span style="float: right; color: #8492a6; font-size: 13px">
                {{ classItem.studentCount }}名学生
              </span>
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      
      <span slot="footer" class="dialog-footer">
        <el-button @click="handleClose">取 消</el-button>
        <el-button type="primary" @click="submitForm" :loading="loading">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { createGroup } from '@/api/chat'
import { getClasses } from '@/api/class'

export default {
  name: 'CreateGroup',
  data() {
    return {
      dialogVisible: false,
      loading: false,
      classList: [],
      groupForm: {
        groupName: '',
        descText: '',
        classId: null
      },
      rules: {
        groupName: [
          { required: true, message: '请输入群组名称', trigger: 'blur' },
          { min: 2, max: 20, message: '长度在 2 到 20 个字符', trigger: 'blur' }
        ],
        descText: [
          { required: true, message: '请输入群组描述', trigger: 'blur' },
          { max: 200, message: '长度不能超过 200 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    async show() {
      this.dialogVisible = true
      await this.loadClasses()
    },
    handleClose() {
      this.dialogVisible = false
      this.resetForm()
    },
    resetForm() {
      this.groupForm = {
        groupName: '',
        descText: '',
        classId: null
      }
      if (this.$refs.groupForm) {
        this.$refs.groupForm.resetFields()
      }
    },
    async loadClasses() {
      try {
        const response = await getClasses()
        if (response.code === 20000) {
          this.classList = response.data
        }
      } catch (error) {
        console.error('加载班级列表失败:', error)
        this.$message.error('加载班级列表失败')
      }
    },
    submitForm() {
      this.$refs.groupForm.validate(async (valid) => {
        if (valid) {
          this.loading = true
          try {
            const response = await createGroup(this.groupForm)
            if (response.code === 20000) {
              this.$message.success('创建群组成功')
              this.$emit('created', response.data)
              this.handleClose()
            } else {
              this.$message.error(response.message || '创建群组失败')
            }
          } catch (error) {
            console.error('创建群组失败:', error)
            this.$message.error('创建群组失败')
          } finally {
            this.loading = false
          }
        }
      })
    }
  }
}
</script>

<style scoped>
.create-group {
  .el-select {
    width: 100%;
  }
}
</style> 